package org.yanghaokun.pet.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.order.domain.AdoptOrder;
import org.yanghaokun.order.mapper.AdoptOrderMapper;
import org.yanghaokun.pet.domain.Pet;
import org.yanghaokun.pet.domain.PetDetail;
import org.yanghaokun.pet.mapper.PetDetailMapper;
import org.yanghaokun.pet.mapper.PetMapper;
import org.yanghaokun.pet.service.IPetService;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.mapper.UserMapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;

    @Override
    public void add(Pet pet) {
        //添加信息我们先添加本身
        petMapper.save(pet);
        //再添加详情信息
        //我们根据本身查找是否有详情，有详情还要非空判断
        PetDetail detail = pet.getDetail();
        if (detail != null){
            //关联两张表
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
    }

    @Override
    public void del(Long id) {
        super.del(id);
    }

    @Override
    public void onsaleOrOffsale(int flag, List<Long> ids) {
        if (flag == 1) {
            Map<String,Object> params = new HashMap<>();
            params.put("ids",ids);
            params.put("onsaletime",new Date());
            petMapper.onsale(params);
        }else{
            //下架
            Map<String,Object> params = new HashMap<>();
            params.put("ids",ids);
            params.put("offsaletime",new Date());
            petMapper.offSale(params);

        }
    }

    @Override
    public void adopt(Long petId, Long loginInfoId) {
        //首先将id设置给宠物信息
        Pet pet = petMapper.findOne(petId);
        //根据登陆信息查找用户
        User user = userMapper.findByloginId(loginInfoId);
        pet.setUser_id(user.getId());
        //修改领养状态
        pet.setState(2);//2为已领养
        petMapper.update(pet);
    }


}
